/**  
* Copyright (c) 2008, Noel Lynch All rights reserved. Redistribution and use  
* in source and binary forms, with or without modification, are permitted  
* provided that the following conditions are met: Redistributions of source  
* code must retain the above copyright notice, this list of conditions and the  
* following disclaimer. Redistributions in binary form must reproduce the above  
* copyright notice, this list of conditions and the following disclaimer in the  
* documentation and/or other materials provided with the distribution. Neither  
* the name of Noel Lynch nor the names of contributors may be used to  
* endorse or promote products derived from this software without specific prior  
* written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR  
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
*/
// Apr 6, 2009

package com.sceneworker.util.swing.radiobutton;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Hashtable;

import com.sun.org.apache.bcel.internal.generic.LUSHR;

public class SimpleRadioButtonManager implements MouseListener {
	
	private		ArrayList<SimpleRadioButton>		mcla_buttons;
	private		SimpleRadioButton					mcl_curSel = null;
	
	public	SimpleRadioButtonManager() {
		mcla_buttons = new ArrayList<SimpleRadioButton>();
	}
	
	public	void	addButton(SimpleRadioButton cl_btn) {
		mcla_buttons.add(cl_btn);
		
		cl_btn.addMouseListener(this);
	}
	
	public	void	setCurrentSelectionByName(String str_name) {
		int		li_cnt = 0;
		
		for(SimpleRadioButton lcl_btn : mcla_buttons) {
			if(lcl_btn.getName().equals(str_name)) {
				setSelectionAt(li_cnt);
				return;
			}
			li_cnt++;
		}
	}
	
	public	void	setSelectionAt(int i_selIndex) {
		SimpleRadioButton		lcl_newSel = null;
		
		try {
			lcl_newSel = mcla_buttons.get(i_selIndex);
		} catch (Exception e) {
			
			return;
		}
		
		// same selection?
		if(mcl_curSel == lcl_newSel) {
			return;
		}
		
		lcl_newSel.setSelected(true);
		
		if(mcl_curSel != null) {
			mcl_curSel.setSelected(false);
			mcl_curSel.repaint();
		}
		
		mcl_curSel = lcl_newSel;
		mcl_curSel.repaint();
	}

	public	void	reset() {
		if(mcl_curSel != null) {
			mcl_curSel.setSelected(false);
			mcl_curSel.repaint();
		}
		
		mcl_curSel = null;
	}
	
	public	void	cycleToNextBtn() {
		int		li_cur = getCurrentSelectionIndex();
		
		if(li_cur == -1) { ;return; }
		
		li_cur++;
		
		li_cur %= mcla_buttons.size();
		
		setSelectionAt(li_cur);
	}
	
	public	void	cycleToPreviousBtn() {
		int		li_cur = getCurrentSelectionIndex();
		
		if(li_cur == -1) { return; }
		
		li_cur--;
		
		if(li_cur < 0) {
			li_cur = mcla_buttons.size() - 1;
		}
		
		setSelectionAt(li_cur);
	}
	
	public	SimpleRadioButton		getCurrentSelection() {
		return mcl_curSel;
	}
	
	public	int		getCurrentSelectionIndex() {
		if(mcl_curSel == null) { return -1; }
		
		int		li_temp = 0;
		
		for(SimpleRadioButton lcl_btn : mcla_buttons) {
			
			if(lcl_btn == mcl_curSel) {
				return li_temp;
			}
			
			li_temp++;
		}
		
		return -1;
	}
	
	public void mouseClicked(MouseEvent e) {
		
		SimpleRadioButton		lcl_rb = (SimpleRadioButton)e.getComponent();
		
		if(lcl_rb == mcl_curSel) { return; }
		
		lcl_rb.setSelected(true);
		lcl_rb.getSelectionListener().handleSelection(this);
		
		if(mcl_curSel != null) {
			mcl_curSel.setSelected(false);
			mcl_curSel.repaint();
		}
		
		mcl_curSel = lcl_rb;
		mcl_curSel.repaint();
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
	}

	public void mouseReleased(MouseEvent e) {
	}
	
	
}
